package controller

import (
	"github.com/gin-gonic/gin"
	"strconv"
	"wish_go/src/main/com/ajie/common"
	"wish_go/src/main/com/ajie/dao"
	"wish_go/src/main/com/ajie/model"
	"wish_go/src/main/com/ajie/result"
	"wish_go/src/main/com/ajie/vo"
)

type ActivityVo struct {
	model.Activity
	vo.Pagination
}

// @Tags 社区活动
// @Summary 分页社区活动
// @Accept json
// @Produce json
// @Param pageSize body int true "第几页"
// @Param pageNum body int true "页数大小"
// @Param sortName body string false "排序字段"
// @Param title body string false "标题"
// @Param content body string false "具体内容"
// @Success 200 {object} result.Result "{"code": 200, "message": "分页查询成功", "rows": [], "total": 0}"
// @Failure 500 {object} result.Result "{ "code": 500, "message": "异常信息" }"
// @Router /activity/page [post]
func PageActivity(req *gin.Context) {
	var av ActivityVo
	// 解析其他参数
	if err := req.BindJSON(&av); err != nil {
		result.Error(req, 500, "参数解析异常")
		return
	}
	pagination := common.GeneratePagination(vo.Pagination{PageSize: av.PageSize, PageNum: av.PageNum, SortName: av.SortName})
	activity := model.Activity{Title: av.Title, Content: av.Content, Grade: av.Grade, BaseModel: model.BaseModel{AreaCode: av.AreaCode}}
	rows, total, err := dao.PageActivity(&activity, &pagination)
	if err != nil {
		result.Error(req, 500, "查询失败")
		return
	}
	result.PageRsult(req, rows, total)
}

// @Tags 社区活动
// @Summary 新增社区活动
// @accept json
// @Produce json
// @Security ApiKeyAuth
// @Param title body string true "标题"
// @Param content body string true "具体内容"
// @Param image body string false "图片地址"
// @Success 200 {object} result.Result "{"code": 200, "message": "操作成功"}"
// @Failure 500 {object} result.Result "{ "code": 500, "message": "异常信息" }"
// @Router /activity/insert [POST]
func InsertActivity(req *gin.Context) {
	activity := model.Activity{}
	// 解析参数为实体json
	if err := req.BindJSON(&activity); err != nil {
		result.Error(req, 500, "参数解析异常："+err.Error())
		return
	}
	user := UserInfo(req)
	if user.Id != 0 {
		activity.AreaCode = user.AreaCode
		activity.CreateName = user.Name
	}
	if dbErr := dao.InsertActivity(activity); dbErr != nil {
		result.Error(req, 500, "添加失败"+dbErr.Error())
		return
	}
	result.Success(req, "添加成功")
}

// @Tags 社区活动
// @Summary 修改社区活动
// @accept json
// @Produce json
// @Security ApiKeyAuth
// @Param id body int true "唯一标识"
// @Param title body string false "标题"
// @Param content body string false "具体内容"
// @Param image body string false "图片地址"
// @Success 200 {object} result.Result "{"code": 200, "message": "操作成功"}"
// @Failure 500 {object} result.Result "{ "code": 500, "message": "异常信息" }"
// @Router /activity/update [PUT]
func UpdateActivity(req *gin.Context) {
	activity := model.Activity{}
	// 解析参数为实体json
	_ = req.BindJSON(&activity)
	user := UserInfo(req)
	if user.Id != 0 {
		activity.UpdateName = user.Name
	}
	if dbErr := dao.UpdateActivity(activity); dbErr != nil {
		result.Error(req, 500, "更新失败"+dbErr.Error())
		return
	}
	result.Success(req, "更新成功")
}

// @Tags 社区活动
// @Summary 删除社区活动
// @Produce json
// @Security ApiKeyAuth
// @Param id path int true "唯一标识"
// @Success 200 {object} result.Result "{"code": 200, "message": "操作成功"}"
// @Failure 500 {object} result.Result "{ "code": 500, "message": "异常信息" }"
// @Router /activity/delete [DELETE]
func DeleteActivity(req *gin.Context) {
	id, _ := strconv.Atoi(req.Param("id"))
	if id == 0 {
		result.Error(req, 500, "id不能为空")
		return
	}
	activity := model.Activity{}
	activity.Id = uint(id)
	if dbErr := dao.DeleteActivity(activity); dbErr != nil {
		result.Error(req, 500, "删除失败"+dbErr.Error())
		return
	}
	result.Success(req, "社区活动删除成功")
}
